<?php
namespace Leads\Model;

use Zend\Db\TableGateway\TableGateway;

class LeadsTables
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }
	
    public function getLeads($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function saveLead(Entity\Person $person)
    {
        $data = array(
            //'id' => $person->id,
            'department'  => $product->department,			
			'firstname' => $product->firstname,
            'jobtitle'  => $product->jobtitle,
			'lastname' => $product->lastname,
            'mobilephone'  => $product->mobilephone,
			'officephone'  => $product->officephone,
			'officefax' => $product->officefax,
            'ownedsecurableitem_id'  => $product->ownedsecurableitem_id,
			'primaryaddress_address_id'  => $product->primaryaddress_address_id,
			'primaryemail_email_id'  => $product->primaryemail_email_id,
			'title_customfield_id' => $product->title_customfield_id,		
        );

        $id = (int)$person->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getPerson($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteLead($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }
}
?>